package com.ttn.common.core.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ConcurrencyTester;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import com.aliyun.openservices.ons.shaded.com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.util.StopWatch;

import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Random;


@Slf4j
public class SocketTest {
    private static WebSocketClient webSocketClient = null;

    public static void main(String[] args) throws IOException {
        List<String> list = Lists.newArrayList("ws://192.168.20.203:8818/?username=771_625843054741295104&password=771_625843054741295104", "ws://192.168" +
                ".20.203:8818/?username=700_620803154665017344&password=700_620803154665017344", "ws://192.168.20" +
                ".203:8818/?username=169_602683506841096192&password=169_602683506841096192", "ws://192.168.20" +
                ".203:8818/?username=771_625531277725929472&password=771_625531277725929472", "ws://192.168.20" +
                ".203:8818/?username=169_592865797014032384&password=169_592865797014032384", "ws://192.168.20" +
                ".203:8818/?username=397_605250416128561152&password=397_605250416128561152", "ws://192.168.20" +
                ".203:8818/?username=180_586025029897031680&password=180_586025029897031680", "ws://192.168.20" +
                ".203:8818?username=169_597241562501484544&password=169_597241562501484544", "ws://192.168.20" +
                ".203:8818?username=169_597239422223650816&password=169_597239422223650816","ws://192.168.20" +
                ".203:8818?username=169_597238728288636928&password=169_597238728288636928","ws://192.168.20" +
                ".203:8818?username=169_597238409152434176&password=169_597238409152434176","ws://192.168.20" +
                ".203:8818?username=169_597238323844485120&password=169_597238323844485120","ws://192.168.20" +
                ".203:8818?username=169_597237569356304384&password=169_597237569356304384");

        StopWatch sw = new StopWatch("5000线程测试");
        sw.start("5000线程测试");
        ConcurrencyTester tester = ThreadUtil.concurrencyTest(5000, () -> {
            try {
                Random random = new Random();
                int num = random.nextInt(list.size());


                WebSocketClient webSocketClient = new WebSocketClient(new URI(list.get(num)), new Draft_6455()) {
                    String str = list.get(num);

                    //连接服务端时触发
                    @Override
                    public void onOpen(ServerHandshake handshakedata) {

                        log.info("websocket客户端和服务器连接成功");

                        String mrs = "{\"readStatus\":0,\"chatType\":2,\"cmd\":11,\"createTime\":" + DateTime.now().getTime() +
                                "," +
                                "\"from\":\"" + str.substring(str.indexOf("?username=") + 10,
                                str.indexOf("&password")) + "\",\"to\":\"10118\",\"chatId\":\"" + str.substring(str.indexOf("?username=") + 10,
                                str.indexOf("&password")) + "/" + DateTime.now().getTime() +
                                "\"," +
                                "\"msgType\":0,\"content\":\"魏颖测试\"}";
                        send(mrs);
                    }

                    //收到服务端消息时触发
                    @Override
                    public void onMessage(String message) {
                        log.info("websocket客户端收到消息={}", message);
                    }

                    //和服务端断开连接时触发
                    @Override
                    public void onClose(int code, String reason, boolean remote) {
                        log.info("websocket客户端退出连接");
                    }

                    //连接异常时触发
                    @Override
                    public void onError(Exception ex) {
                        log.info("websocket客户端和服务器连接发生错误={}", ex.getMessage());
                    }
                };
                long delay = RandomUtil.randomLong(100, 1000);
                ThreadUtil.sleep(delay);
                webSocketClient.connect();

            } catch (Exception e) {
                e.printStackTrace();
            }

            // 测试的逻辑内容
//            long delay = RandomUtil.randomLong(1, 1);
//            ThreadUtil.sleep(delay);
//            Console.log("{} test finished, delay: {}", Thread.currentThread().getName(), delay);

        });
        sw.stop();
        System.out.println("sw.prettyPrint()~~~~~~~~~~~~~~~~~");
        System.out.println(sw.prettyPrint());

// 获取总的执行时间，单位毫秒
        Console.log(tester.getInterval());

    }


}
